/*
                    Checkbox Class Implementation
*/
#include "Checkbox.h"

Checkbox::Checkbox()
{
          bChecked = false;
          bVisible = true;
          bEnabled = true;
          strText = "";

          BoxImageUnchecked = NULL;
          BoxImageChecked = NULL;

          Position.x = 0;
          Position.y = 0;
}

Checkbox::~Checkbox()
{
    SDL_FreeSurface(BoxImageUnchecked);
    SDL_FreeSurface(BoxImageChecked);
}

int Checkbox::Initialise(Point NewPosition, string strFFilepath, int iSize,
                                   string strCFilepath, string strNCFilepath, string strLabelText, SDL_Color NewColor)
{
     lblText.Initialise(Point(0, 0), strFFilepath, iSize, strLabelText, NewColor);

     SDL_FreeSurface(BoxImageUnchecked);
     SDL_FreeSurface(BoxImageChecked);

     BoxImageUnchecked = OptimiseAlphaSurface(IMG_Load(strNCFilepath.c_str()));
     BoxImageChecked = OptimiseAlphaSurface(IMG_Load(strCFilepath.c_str()));

     Position = NewPosition;

     UpdateCheckbox();

     return 0;
}

int Checkbox::UpdateCheckbox()
{
    if (bChecked == true)
    {
         lblText.setPosition(Point(Position.x + (BoxImageChecked->w + 5), Position.y));
    }
    else
    {
        lblText.setPosition(Point(Position.x + (BoxImageUnchecked->w + 5), Position.y));
    }

    return 0;
}

int Checkbox::SwitchChecked()
{
    if (bChecked == true) { bChecked = false; }
    else if (bChecked == false) { bChecked = true; }

    UpdateCheckbox();

    return 0;
}

void Checkbox::setChecked(bool bNewChecked)
{
     bChecked = bNewChecked;

     UpdateCheckbox();
}

bool Checkbox::getChecked()
{
     return bChecked;
}

void Checkbox::setPosition(Point NewPosition)
{
     Position = NewPosition;

     UpdateCheckbox();
}

Point Checkbox::getPosition()
{
       return Position;
}

int Checkbox::HandleMouseDown(int iMouseX, int iMouseY)
{
    int iReturnVal = 0;

    double xpos = Position.x;
    double ypos = Position.y;
    int iWidth = 0;
    int iHeight = 0;

    if (bChecked == true)
    {
        iWidth = BoxImageChecked->w;
        iHeight = BoxImageChecked->h;
    }
    else
    {
        iWidth = BoxImageUnchecked->w;
        iHeight = BoxImageUnchecked->h;
    }

    if((iMouseX > xpos) && (iMouseX < xpos + iWidth) && (iMouseY > ypos) && (iMouseY < ypos + iHeight))
    {
        iReturnVal = 1;
        SwitchChecked();
    }

    xpos = lblText.getPosition().x;
    ypos = lblText.getPosition().y;
    iWidth = lblText.getWidth();
    iHeight = lblText.getHeight();

    if((iMouseX > xpos) && (iMouseX < xpos + iWidth) && (iMouseY > ypos) && (iMouseY < ypos + iHeight))
    {
        iReturnVal = 1;
        SwitchChecked();
    }

    return iReturnVal;
}

int Checkbox::getWidth()
{
    if (bChecked == true)
    {
         return (BoxImageChecked->w + lblText.getWidth());
    }
    else
    {
        return (BoxImageUnchecked->w + lblText.getWidth());
    }
}

int Checkbox::BlitToSurface(SDL_Surface *screen)
{
    if (bVisible == true)
    {
        SDL_Rect dest;
        dest.x = static_cast<int>(Position.x);
        dest.y = static_cast<int>(Position.y);

        if (bChecked == true)
        {
           SDL_BlitSurface(BoxImageChecked, NULL, screen, &dest);
        }
        else
        {
            SDL_BlitSurface(BoxImageUnchecked, NULL, screen, &dest);
        }

        lblText.BlitToSurface(screen);
    }

    return 0;
}

int Checkbox::BlitToFrame(SDL_Surface *surface, Point FramePosition)
{
    if (bVisible == true)
    {
        SDL_Rect dest;
        dest.x = static_cast<int>(Position.x) + static_cast<int>(FramePosition.x);
        dest.y = static_cast<int>(Position.y) + static_cast<int>(FramePosition.y);

        if (bChecked == true)
        {
           SDL_BlitSurface(BoxImageChecked, NULL, surface, &dest);
        }
        else
        {
            SDL_BlitSurface(BoxImageUnchecked, NULL, surface, &dest);
        }

        lblText.BlitToFrame(surface, FramePosition);
    }

    return 0;
}
